#include <QDir>
#include "Entry.h"


// RecordItem

RecordItem::RecordItem(QDomElement source) : SharedDomObject(source)
{
}

RecordItem::RecordItem(TFile file) : SharedDomObject(file.m_source)
{
}

RecordItem::RecordItem(TDir dir) : SharedDomObject(dir.m_source)
{
}

bool RecordItem::isDir()
{
    return (m_source.tagName() == TDir::tagName());
}

bool RecordItem::isFile()
{
    return (m_source.tagName() == TFile::tagName());
}

bool RecordItem::isVaild()
{
    return (isDir() || isFile());
}

TDir RecordItem::toDir()
{
    if (isDir())
        return TDir(m_source);
    else
        return TDir(QDomElement());
}

TFile RecordItem::toFile()
{
    if (isFile())
        return TFile(m_source);
    else
        return TFile(QDomElement());
}

QString RecordItem::name()
{
    return m_source.attribute("name");
}

void RecordItem::setName(const QString &name)
{
    m_source.setAttribute("name", name);
}

// TFile

TFile::TFile(QDomElement source) : SharedDomObject(source)
{

}

QString TFile::name()
{
    return m_source.attribute("name");
}

quint64 TFile::size()
{
    return m_source.attribute("size").toULongLong();
}

QString TFile::qs()
{
    return m_source.attribute("qs");
}

int TFile::relationId()
{
    return m_source.attribute("id").toInt();
}

void TFile::setRelationId(int id)
{
    m_source.setAttribute("id", id);
}

void TFile::setName(const QString &name)
{
    m_source.setAttribute("name", name);
}

void TFile::setSize(quint64 size)
{
    m_source.setAttribute("size", size);
}

void TFile::setQs(const QString &qs)
{
     m_source.setAttribute("qs", qs);
}

// TDir

TDir::TDir(QDomElement source) : RecordList(source)
{

}

TFile TDir::createNewFile(bool appendToList)
{
    TFile child = m_source.ownerDocument().createElement(TFile::tagName());
    if (appendToList) {
        append(child);
    }
    return child;
}

TDir TDir::createNewDir(bool appendToList)
{
    TDir child = m_source.ownerDocument().createElement(TDir::tagName());
    if (appendToList) {
        append(child);
    }
    return child;
}

RecordItem TDir::findChildByName(const QString &name)
{
    QDomElement child = m_source.firstChildElement();
    while (!child.isNull()) {
        if (child.attribute("name") == name)
            break;
        else
            child = child.nextSiblingElement();
    }
    return RecordItem(child);
}

QString TDir::name()
{
    return m_source.attribute("name");
}

int TDir::relationId()
{
    return m_source.attribute("id").toInt();
}

void TDir::setRelationId(int id)
{
    m_source.setAttribute("id", id);
}

void TDir::setName(const QString &name)
{
    m_source.setAttribute("name", name);
}

// Entry

Entry::Entry(QDomElement source) : TDir(source)
{

}

TFile Entry::createNewFile(const QString &filePath)
{
    if (filePath.isEmpty()) {
        return QDomElement();
    }

    QString path = QDir::fromNativeSeparators(filePath);
    int index = path.lastIndexOf("/");
    QString dirPath = path.left(index);
    QString fileName = path.mid(index + 1);

    TDir current = (index >= 0 ? createNewDir(dirPath) : m_source);
    RecordItem item = current.findChildByName(fileName);
    if (item.isNull()) {
        item = current.createNewFile();
        item.setName(fileName);
    }
    return item.toFile();
}

TDir Entry::createNewDir(const QString &dirPath)
{
    if (dirPath.isEmpty()) {
        return QDomElement();
    }

    QStringList nameList = QDir::fromNativeSeparators(dirPath).split("/", QString::SkipEmptyParts);
    TDir current = m_source;
    for (int i = 0; i < nameList.size(); i++) {
        RecordItem item = current.findChildByName(nameList[i]);
        if (item.isNull()) {
            TDir dir = current.createNewDir();
            dir.setName(nameList[i]);
            current = dir;
        } else if (item.isDir()) {
            TDir dir = item.toDir();
            current = dir;
        } else if (item.isFile()) {
            return QDomElement();
        }
    }
    return current;
}

QString Entry::path()
{
    return m_source.attribute("path");
}

void Entry::setPath(const QString &path)
{
    m_source.setAttribute("path", path);
}

